﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FormChangePass : Form
    {
        tbl_usrTableAdapter usr = new tbl_usrTableAdapter();
        public FormChangePass()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string password = tbxorg.Text;
            password = GetMd5Str(password);
            
                
                    if (tbxnew.Text == tbxnew2.Text)
                    {
                        string newpass = GetMd5Str(tbxnew.Text);
                        int passcount = int.Parse(usr.GetPasswordCount(password).ToString());
                        if (passcount>0)
                        {
                            usr.UpdatePassWordByOrgPass(newpass,password);
                            MessageBox.Show("修改密码成功！");
                            this.Close();

                        }else
                        {
                            lbtip.Text = "您输入的原始密码不对，请重新输入";
                        }
                        
                        

                    }
                    else
                    {

                        lbtip.Text = "第一次和第二次输入的密码不一致";
                    }
              

        }


        public static string GetMd5Str(string orgpass)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string pass = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(orgpass)), 4, 8);
            pass = pass.Replace("-", "");
            return pass;
        }

        private void tbxnew2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string password = tbxorg.Text;
                password = GetMd5Str(password);
                if (tbxnew.Text == tbxnew2.Text)
                {
                    string newpass = GetMd5Str(tbxnew.Text);
                    int passcount = int.Parse(usr.GetPasswordCount(password).ToString());
                    if (passcount > 0)
                    {
                        usr.UpdatePassWordByOrgPass(newpass, password);
                        MessageBox.Show("修改密码成功！");
                        this.Close();

                    }
                    else
                    {
                        lbtip.Text = "您输入的原始密码不对，请重新输入";
                    }



                }
                else
                {

                    lbtip.Text = "第一次和第二次输入的密码不一致";
                }

            }
        }


    }
}
